package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.BaseSignListResponse
import com.gexiaobao.pigeon.databinding.ItemSignPassBinding

/**
 * @Author      : hxw
 * @Date        : 2024/7/16 14:31
 * @Describe    :
 */
class AdapterPassSign(data: ArrayList<BaseSignListResponse.SignPigeonListBean>) :
    BaseQuickAdapter<BaseSignListResponse.SignPigeonListBean, DataBindBaseViewHolder>(R.layout.item_sign_pass, data) {

    private var mOnItemClickListener: OnItemCheckClickListener? = null
    private val checkList = ArrayList<BaseSignListResponse.SignPigeonListBean>()

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(holder: DataBindBaseViewHolder, item: BaseSignListResponse.SignPigeonListBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemSignPassBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        if (holder.layoutPosition % 2 == 0) {//偶数
            holder.setBackgroundColor(R.id.llItem, ContextCompat.getColor(context, R.color.comm_bg))
        } else {
            holder.setBackgroundColor(R.id.llItem, ContextCompat.getColor(context, R.color.white))
        }

        val isDisable: ArrayList<Boolean> = arrayListOf()
        val isCheck: ArrayList<Boolean> = arrayListOf()
        val ring = data[getItemPosition(item)]

        if (item.signStatus.isNotEmpty()) {
            item.signStatus.forEach {
                isDisable.add(it.isDisAble)
                isCheck.add(it.isCheck)
            }
            if (isDisable.contains(false)) {
                if (isCheck.contains(true)) {
                    holder.setImageResource(R.id.ivItemSignChecked, R.mipmap.icon_sign_checked)
                } else {
                    holder.setImageResource(R.id.ivItemSignChecked, R.mipmap.icon_sign_unchecked)
                }
                item.isDisAble = true
            } else {//如果signStatus中的项目 isDisAble的值都是true，则设置当前赛鸽不可点击  不可报名
                item.isDisAble = false
                holder.setImageResource(R.id.ivItemSignChecked, R.mipmap.icon_disable_group)
            }
        }

        holder.itemView.setOnClickListener {
            val isCheckedItem = item.isChecked
            if (data[getItemPosition(item)].isChecked) {
                ring.isChecked = false
                if (item.signStatus.isNotEmpty()) {
                    item.signStatus.forEach {
                        it.isCheck = false
                    }
                }
                notifyItemChanged(getItemPosition(item))
            }
//            如果是未选中状态，暂不做操作
//            else {
//                if (item.signStatus.isNotEmpty()) {
//                    item.signStatus.forEach {
//                        it.isCheck = true
//                    }
//                }
//                ring.isChecked = true
//                notifyItemChanged(getItemPosition(item))
//            }
            checkList.clear()
            data.forEach {
                if (it.isChecked) {
                    checkList.add(it)
                } else {
                    checkList.remove(it)
                }
            }
            mOnItemClickListener?.onItemClick(isCheckedItem, getItemPosition(item), data)//checkList-选中的list，
        }
    }

    fun setOnMultipleChoiceClickListener(listener: OnItemCheckClickListener?) {
        mOnItemClickListener = listener
    }

    interface OnItemCheckClickListener {
        fun onItemClick(isCheckedItem: Boolean, position: Int, list: List<BaseSignListResponse.SignPigeonListBean>)
    }
}